<template>
  <el-drawer size="80%" :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="平台名称" prop="platform">
            <el-input v-model="formData.platform" placeholder="请输入平台名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
         <el-select v-model="formData.category" placeholder="请选择字典">
          <el-option
            v-for="item in getIntDictOptions(DICT_TYPE.AGENT_ACCOUNT_CATEGORY)"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="账号名称" prop="accountName">
            <el-input v-model="formData.accountName" placeholder="请输入账号名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="formData.username" placeholder="请输入用户名/邮箱" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="密码" prop="password">
        <el-input
          v-model="formData.password"
          placeholder="请输入密码"
        />
      </el-form-item>

      <el-form-item label="用途说明" prop="description">
        <el-input
          v-model="formData.description"
          type="textarea"
          :rows="3"
          placeholder="请输入用途说明"
        />
      </el-form-item>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="联系人" prop="contact">
            <el-input v-model="formData.contact" placeholder="请输入联系人" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系电话" prop="phone">
            <el-input v-model="formData.phone" placeholder="请输入联系电话" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="到期时间" prop="expireDate">
            <el-date-picker
              v-model="formData.expireDate"
              type="date"
              placeholder="请选择到期时间"
              style="width: 100%"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="formData.status">
              <el-radio label="正常">正常</el-radio>
              <el-radio label="过期">过期</el-radio>
              <el-radio label="停用">停用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="其他内容" prop="context">
        <el-input
          v-model="formData.context"
          type="textarea"
          :rows="4"
          placeholder="请输入相关内容"
        />
      </el-form-item>

      <!-- <el-form-item label="平台Logo" prop="logo">
        <el-input v-model="formData.logo" placeholder="请输入平台Logo地址" />
      </el-form-item> -->

      <el-form-item label="备注信息" prop="remark">
        <el-input
          v-model="formData.remark"
          type="textarea"
          :rows="2"
          placeholder="请输入备注信息"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts">
import { AccountApi, type AccountVO } from '@/api/agent/manage/account'
import {DICT_TYPE, getIntDictOptions} from "@/utils/dict";
/** 第三方账号管理 表单 */
defineOptions({ name: 'AccountForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  platform: undefined,
  category: undefined,
  accountName: undefined,
  username: undefined,
  password: undefined,
  description: undefined,
  contact: undefined,
  phone: undefined,
  expireDate: undefined,
  status: '正常',
  context: undefined,
  remark: undefined,
  logo: undefined
})
const formRules = reactive({
  platform: [{ required: true, message: '平台名称不能为空', trigger: 'blur' }],
  category: [{ required: true, message: '分类不能为空', trigger: 'change' }],
  accountName: [{ required: true, message: '账号名称不能为空', trigger: 'blur' }],
  username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
  password: [{ required: true, message: '密码不能为空', trigger: 'blur' }],
  description: [{ required: true, message: '用途说明不能为空', trigger: 'blur' }],
  contact: [{ required: true, message: '联系人不能为空', trigger: 'blur' }],
  // phone: [
  //   { required: true, message: '联系电话不能为空', trigger: 'blur' },
  //   { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  // ]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await AccountApi.getAccount(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as AccountVO
    if (formType.value === 'create') {
      await AccountApi.createAccount(data)
      message.success(t('common.createSuccess'))
    } else {
      await AccountApi.updateAccount(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    platform: undefined,
    category: undefined,
    accountName: undefined,
    username: undefined,
    password: undefined,
    description: undefined,
    contact: undefined,
    phone: undefined,
    expireDate: undefined,
    status: '正常',
    context: undefined,
    remark: undefined,
    logo: undefined
  }
  formRef.value?.resetFields()
}
</script>